package com.mahesh;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Service {
	Connection conn = null;
	ResultSet rs = null;

	DBConnection dbconn = new DBConnection();
	MailMang mmg= new MailMang();

	public boolean authenicate(String username, String password) {

		try {
			String query="select email,password from userdetails";;
			rs = dbconn.select(query);

			while (rs.next()) {

				if ((username.equals(rs.getString(1)))
						&& (((String) password).equals((String) (rs
								.getString(2))))) {
					return true;
				}
			}
		} catch (SQLException e1) {
			e1.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;

	}
	
	public boolean existingUser(String email){
		try {
			String query= "Select email from userdetails";
			rs = dbconn.select(query);
			System.out.println("Entering existing User block");

			while (rs.next()) {
				System.out.println("while");
				if (email.equals(rs.getString(2))) {
					System.out.println("Already Existed");
					return false;
				}
			}
		
		} catch (SQLException e1) {
			e1.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return true;
		}
	

	public boolean saveUserDetails(String name, String email, Long mob,
			String pwd) {
		boolean res = dbconn.insert(name, email, mob, pwd);
		return res;
	}

	public void mailing(String email, String fullname) {
		mmg.SendMail(email, fullname);
		
	}
}
